package com.yonyou.pmclouds.safeinspection.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.AdditionalPropertiesVOWrapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.safeinspection.entity.SafeInspectionListVO;
import com.yonyou.pmclouds.safeinspection.entity.SafeInspectionTransConstant;
import com.yonyou.pmclouds.safeinspection.entity.SafeInspectionVO;
import com.yonyou.pmclouds.safeinspection.rmiitf.SafeinspectionMaintainService;
import com.yonyou.pmclouds.safeinspection.rmiitf.SafeinspectionQueryService;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolDetailVO;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping({"/mobile/safeinspect","/safe"})
public class SafeinspectMobController {

    //这边需要做一个creator中友互通的字段翻译

    @Reference(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass = SafeinspectionQueryService.class)
    private SafeinspectionQueryService queryService;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass = SafeinspectionMaintainService.class)
    private SafeinspectionMaintainService maintainService;
    @Resource
    private UserInfoQuery userInfoQuery;
    @Resource
    private CommonPropertiesProcessor commonPropertiesProcessor;

    //参照引入 主表展示
    @RequestMapping("/querystandard")
    public List<SafetypatrolVO> querystandard(@RequestParam(value = "pkTenant", required = false) String pkTenant) throws BusinessException {
        if (StringUtils.isEmpty(pkTenant)){
           pkTenant = RuntimeEnvironment.getTenantId();
        }
        return queryService.queryByTenant(pkTenant);

    }

    //参照引入 子表展示
    @RequestMapping("/querydetail")
    public List<SafetypatrolDetailVO> querydetail(@RequestParam(value = "pkSafetypatrol") String pkSafetypatrol) throws BusinessException {
        if (StringUtils.isEmpty(pkSafetypatrol)){
           return null;
        }
        return queryService.queryBypkSafetypatrol(pkSafetypatrol);

    }


    //列表态展示吗 确定一下 TODO
    @RequestMapping("/findlistdetail")
    public AdditionalPropertiesVOWrapper[] findlistdetail(@RequestParam("pk_project") String pk_project) throws BusinessException {
        List<SafeInspectionListVO> listVOS = queryService.queryByPkProject(pk_project);
        if (listVOS==null){
            return null;
        }
        SafeInspectionListVO[] safeInspectionListVO =listVOS.toArray(new SafeInspectionListVO[listVOS.size()]);
        //这个没跑通 userInfoQuery查不到值
        userInfoQuery.tranUserInfoField(safeInspectionListVO, new String[]{SafeInspectionTransConstant.CREATOR}, new String[]{SafeInspectionTransConstant.CREATOR_NAME});

        return commonPropertiesProcessor.wrapAdditionalProps(safeInspectionListVO);
    }

    //从列表态点进去展示全部信息
    @RequestMapping("/findvodetail")
    public SafeInspectionVO findvodetail(String pk_safeinspection) throws BusinessException {
        SafeInspectionVO vo = queryService.queryByOne(pk_safeinspection);
        return vo;
    }

    //从展示全部信息点进去查看检查的细节
    @RequestMapping("/findinspectitemdetail")
    public InspectItemVO findinspectitemdetail(String pk_inspectitem) throws BusinessException {
        InspectItemVO inspectItemVO = queryService.queryItemVo(pk_inspectitem);
        return inspectItemVO;
    }


    //添加操作
    @RequestMapping("/add")
    public SafeInspectionVO add(@RequestBody SafeInspectionVO safeInspectionVO) throws BusinessException {
        SafeInspectionVO add = maintainService.add(safeInspectionVO);
        userInfoQuery.tranUserInfoField(new SafeInspectionVO[]{add}, new String[]{SafeInspectionTransConstant.CREATOR}, new String[]{SafeInspectionTransConstant.CREATOR_NAME});
        return add;
    }

    //单据驳回后的修改操作
    @RequestMapping("/update")
    public SafeInspectionVO update(@RequestBody SafeInspectionVO safeInspectionVO) throws BusinessException {
        SafeInspectionVO update = maintainService.update(safeInspectionVO);
        userInfoQuery.tranUserInfoField(new SafeInspectionVO[]{update}, new String[]{SafeInspectionTransConstant.CREATOR}, new String[]{SafeInspectionTransConstant.CREATOR_NAME});
        return update;
    }

    //审批操作--通过
    @RequestMapping("/enableapprove")
    public SafeInspectionVO enableapprove(String pk_safeinspection, byte billStatus) throws BusinessException {
        SafeInspectionVO approve = maintainService.approve(pk_safeinspection, billStatus);
        return approve;
    }

    //审批操作--驳回
    @RequestMapping("/enablereject")
    public SafeInspectionVO enablereject(String pk_safeinspection) throws BusinessException {
        SafeInspectionVO reject = maintainService.reject(pk_safeinspection);
        return reject;
    }
}
